Algorithms

An algorithm is a set of well defined steps for solving a problem. The algorithm indicates the control flow of the solution, or what steps are repeated and where conditions must be tested to indicate which code to execute next in the solution of a problem. There may be many different algorithms for solving the same problem. Algorithms that solve the same problem are often compared to each other to choose the best algorithm to implement. Some criteria for comparing algorithms are:

Elegant algorithms are algorithms that would score high on most if not all of the above criteria. Professional programmers understand which criteria are most important for their particular program tasks and their experience helps them choose the algorithm that best meets their needs. As novice programmers, it is important to learn to identify different algorithms and to be able to evaluate the efficiency of each algorithm before choosing which algorithm to implement. Then, implement the best algorithm you know for your particular problem and continue to learn new algorithms as you gain experience.

We often describe algorithms using pseudo-code. Pseudo-code is English (at least for us) that looks a little like program code. It is easier to understand than program code and it better illustrates the step-by-step nature of a solution than paragraphs written in English.

Example: Pseudo-code algorithm for calculating several persons' BMI

  1. Store the number of persons (can get from user input or read from file, etc.)
  2. Store a value that indicates if the data will be entered in standard or metric units.
  3. For each person:
    1. Store the height of the person.
    2. Store the weight of the person.
    3. If data is in standard units:
      • convert values to metric units.
    4. Calculate the BMI as weight / height2.
    5. Store the results.
    6. [Optional] Display the results to the user as program runs.
  4. [Optional] Write results to a file.

Once we have an algorithm for solving a problem, we can implement (code) it in Matlab or any other suitable programming environment.

Algorithms that solve complex problems like network traffic flow, and circuit design are much more complex then a single list of steps. They have many conditional and iterative statements and may be broken into smaller functions of a much larger solution.

For more information about algorithms, read the http://en.wikipedia.org/wiki/Algorithm entry.